
let callbackList = []
function subscribe(callback) {
  callbackList.push(callback)
  return callbackList.length - 1  // 订阅返回当前索引值，用来取消订阅方法使用
}

function dispatch(data) {
  for(let i in callbackList) {
    callbackList[i] && callbackList[i](data)
  }
}

function clearSubscribe(index) {
  console.log('取消订阅')
  callbackList.splice(index, 1)
}

export { subscribe, dispatch, clearSubscribe }